// pages/message/message.js
import {
  request2
} from "../../utils/request2";

// var util = import('../../utils/util.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    messageList:{},
    isNull: true,
    nullDataIcon:'/images/icon/nullData.jpg',
    x: 0,
    y: 0,
    showModal: false,
    receiverIndex: '#',
    sender: '',
    receiver: '',
    content: '',
    createtime: '',
    handler: '',
    dueTime: '',
    thisUser: ''
  },

  // 消息接收者选择器
  bindPickerChange: function(e) {
    // console.log(e)
    this.setData({
      receiverIndex: e.detail.value,
    })
    var receiverList = this.data.receiverList;
    var receiverIndex = this.data.receiverIndex;
    
    this.setData({
      receiver: receiverList[receiverIndex].name
    })
  },
  // 点击 "+" 按钮触发事件,显示自定义弹窗
  buildNewTalk(e){
    // console.log(e)
    // 获取处理人列表
    request2({
      url: "/servlet/user/list",
      data: {}
    })
    .then(result => {
      console.log(result) 
      this.setData({
        receiverList: result.data.rows
      })
    })
    .catch(err => {}) 
    // console.log(this.data.receiverList)
    this.setData({
      showModal: true
    })
  },
  //  点击返回按钮隐藏
  back: function(){
    this.setData({
      showModal:false,
      receiverIndex: '#'
    })
  },
  // 获取input输入值
  wish_put: function(e){
    this.setData({
      content: e.detail.value
    })
  },
  // 点击 "发送" 按钮发送消息,并关闭弹窗
  sendMsg: function(){
    let persInfo = wx.getStorageSync('persInfo');
    var sender = persInfo.name;
    var receiver = this.data.receiver;
    var content = this.data.content;
    var createtime = util.formatTime(new Date());
    // 选择了接收者才能发送
    if(receiver != '' && content != ''){
    // 发送请求
    request2({
      url: "/servlet/message/save",
      method: 'POST',
      data: {
        sender: sender,
        receiver: receiver,
        content: content,
        createtime: createtime,
      } 
    })
    .then(result => { 
      console.log(result)
    // 显示发送结果提示
      wx.showToast({
        title: '发送成功',
        icon: "success",
      }) 
      this.setData({
        receiver: '',
        content: '',
        receiverIndex: '#'
      })
      this.onShow();
      // 关闭弹窗
      this.setData({
        showModal:false,
      })

    })
    .catch(err => {
      // 显示发送结果提示
      wx.showToast({
        title: '发送失败',
        icon: "error",
      }) 
    })  
    } else if(receiver == ''){
      wx.showToast({
        title: '未选择接收者',
        icon: 'error'
      })
    } else if(content == ''){
      wx.showToast({
        title: '消息内容为空',
        icon: 'error'
      })
    }
  },

  // 催促消息点击查看对应任务详情 
  toTaskDetail: function(e){
    console.log(e)
    let messageIndex = e.currentTarget.dataset.index
    let id = this.data.messageList[messageIndex].content.substring(16)
    let msgId = this.data.messageList[messageIndex].id
    request2({
      url: "/servlet/task/query",
      data: {
        page: 1,
        rows: 99,
        status: 2,
        id: id
      }
    })
    .then(result => {
      console.log(result)
      let typeCode = result.data.data.rows[0].type;
      let deviceCode = result.data.data.rows[0].deviceCode;
      wx.navigateTo({
        //要跳转到的页面路径
        url: '/pages/rrdetails/rrdetails?id='+id+
        '&code='+3001+
        '&typeCode='+typeCode+
        '&showDeviceInfo='+true+
        '&deviceCode='+deviceCode+
        '&whichClick='+3+
        '&msgId='+msgId
      })
    })
    .catch(err => {
      wx.showToast({
        title: '失败',
        icon: 'error',
        duration: 1000
      })
    })  


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 关闭小红点
    wx.hideTabBarRedDot({
      index: 1,
    }); 
    const pages = getCurrentPages()
    const perpage = pages[pages.length - 1]
    perpage.onLoad(this.options)  

  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

    /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    // console.log(options)
    let persInfo = wx.getStorageSync('persInfo')
    var name = persInfo.name
    this.setData({
      thisUser: name
    })
    // 加载消息列表 
    request2({ 
      url: "/servlet/message/query",
      data: {
        page: 1,
        rows: 99,
        name: name 
      } 
    })
    .then(result => { 
      console.log(result) 
      var messageList = result.data.rows 
      var row = result.data.rows.length
      this.setData({
        messageList: messageList
      })
      if(row > 0){
        this.setData({
          isNull: false,
        })
      }
    })
    .catch(err => {})  
  },
})